package com.test.kotlin.ui.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageView
import android.widget.TextView
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ScreenUtils
import com.blankj.utilcode.util.SizeUtils
import com.bumptech.glide.Glide
import com.test.kotlin.R
import com.test.kotlin.bean.LinkData


class LinkAdapter : BaseAdapter {
    var viewCount:Int=0
    var linkData:MutableList<LinkData> = mutableListOf()
    var context: Context
    var isLookMore=true

    constructor(context:Context){
        this.context=context
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        var holder:LinkViewHolder
        var type=getItemViewType(position)
        lateinit var view:View
        //if(convertView==null) {
            view = LayoutInflater.from(context).inflate(
                if (type == 0) R.layout.layout_home_link_item else R.layout.layout_home_link_more_item,
                parent, false
            )
            holder= LinkViewHolder(view,type)
 //           view.setTag(R.id.tag_id,holder)
//        }else{
//            view=convertView
//            holder=view.getTag(R.id.tag_id) as LinkViewHolder
//        }

        when(type){
            0x0000 -> {
                linkData?.let {
                    if(position<it.size){
                        with(it[position]) {
                            Glide.with(holder.ivLinkImg)
                                .load(file_root)
                                .placeholder(R.drawable.place_holder)
                                .into(holder.ivLinkImg)
                        }
                    }
                }
            }
            0x0001 -> {
                holder.tvLinkMore.text=if(isLookMore) "查看更多" else "收起"
                holder.tvLinkMore.requestFocus()
                holder.tvLinkMore.setOnClickListener {
                    if(isLookMore){
                        linkData?.let {
                            viewCount=it.size+1
                            isLookMore=false
                            notifyDataSetChanged()
                        }
                    }else{
                        viewCount=6
                        isLookMore=true
                        notifyDataSetChanged()
                    }
                }
            }
        }

        return view
    }

    override fun getItem(position: Int): Any {
        return linkData[position]
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getCount(): Int {
        return viewCount
    }

    fun updateLinkData(datas:MutableList<LinkData>?){
        datas?.let {
            viewCount=if(it.size>6){
                isLookMore=true
                6
            } else it.size
            if(linkData.isNotEmpty()){
                linkData.clear()
            }
            linkData.addAll(datas)
            //LogUtils.e("viewCount=$viewCount")
            notifyDataSetChanged()
        }
    }

    override fun getItemViewType(position: Int): Int {
        if(viewCount>=6&&position==viewCount-1){
            if(viewCount==6&&isLookMore){
                return 0x0001
            }else if(viewCount>6){
                return 0x0001
            }
        }
        return 0
    }

    class LinkViewHolder{
        lateinit var ivLinkImg:ImageView
        lateinit var tvLinkMore: TextView

        constructor(view:View,type:Int){
            view.layoutParams.width=(ScreenUtils.getScreenWidth()- SizeUtils.dp2px(32f))/3
            view.layoutParams.height=view.layoutParams.width/2
            view.requestLayout()
            when(type){
                0x0000 -> ivLinkImg=view as ImageView
                0x0001 -> tvLinkMore=view as TextView
            }
        }
    }
}